package com.chuangjiangx.member.stored.ddd.query;

import com.chuangjiangx.commons.page.PagingResult;
import com.chuangjiangx.member.stored.dao.mapper.InMbrOrderMapper;
import com.chuangjiangx.member.stored.dao.model.InMbrOrder;
import com.chuangjiangx.member.coupon.ddd.domain.model.MbrCoupon;
import com.chuangjiangx.member.coupon.ddd.domain.repository.MbrCouponRepository;
import com.chuangjiangx.member.basic.ddd.query.MemberQuery;
import com.chuangjiangx.member.stored.ddd.dal.condition.MbrOrderDetailCondition;
import com.chuangjiangx.member.stored.ddd.dal.condition.MbrRefundDetailstCondition;
import com.chuangjiangx.member.stored.ddd.dal.mapper.MbrOrderDalMapper;
import com.chuangjiangx.member.stored.ddd.dal.dto.MbrOrderRechargeResult;
import com.chuangjiangx.member.stored.ddd.dal.dto.MbrOrderDetail;
import com.chuangjiangx.member.stored.ddd.dal.condition.MbrOrderListCondition;


import com.chuangjiangx.member.stored.ddd.dal.dto.MbrRefundDetail;
import com.chuangjiangx.member.stored.ddd.dal.dto.MbrOrderList;
import com.chuangjiangx.member.basic.ddd.query.dto.MemberDTO;
import com.chuangjiangx.member.coupon.ddd.domain.model.MbrCouponId;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 会员消费记录查询
 */
@Component
public class MbrOrderQuery {

    @Autowired
    private InMbrOrderMapper inMbrOrderMapper;
    @Autowired
    private MbrOrderDalMapper mbrOrderDalMapper;
    @Autowired
    private MbrCouponRepository mbrCouponRepository;
    @Autowired
    private MemberQuery memberQuery;



    /**
     * app会员订单列表
     * @param condition 条件
     * @return 结果
     */
    public PagingResult<InMbrOrder> appMbrOrderList(MbrOrderListCondition condition) {
        Objects.requireNonNull(condition.getMerchantId(),"商户id不能为空");
        PagingResult<InMbrOrder> result = new PagingResult<>(condition.getPageNumber(),condition.getPageSize());
        Integer total = mbrOrderDalMapper.countMbrOrder(condition);
        result.setTotal(total);
        if (total > 0) {
            List<InMbrOrder> list = mbrOrderDalMapper.appMbrOrderList(condition);
            List<InMbrOrder> last = new ArrayList<>();
            list.forEach(mbrOrder -> {
                MemberDTO memberDTO = memberQuery.queryByMemberId(mbrOrder.getMemberId());
                if (memberDTO != null) {
                    mbrOrder.setPhoneNumber(memberDTO.getMobile());
                    last.add(mbrOrder);
                } else {
                    last.add(mbrOrder);
                }
            });
            result.setItems(last);
        }
        return result;
    }

    /**
     * app获取会员订单详情
     * @param mbrOrderDetailCondition
     * @return 详情
     */
    public MbrOrderDetail appMbrOrderDetail(MbrOrderDetailCondition mbrOrderDetailCondition) {
        Objects.requireNonNull(mbrOrderDetailCondition.getOrderNumber(),"订单编号不能为空");
        MbrOrderDetail mbrOrderDetail = mbrOrderDalMapper.getMbrOrderDetail(mbrOrderDetailCondition);
        if (null == mbrOrderDetail) {
            return null;
        }
        List<MbrRefundDetail> refundInfos = mbrOrderDalMapper.getMbrOrderRefundInfos(mbrOrderDetail.getId());
        mbrOrderDetail.setRefundDetails(refundInfos);
        MbrCoupon coupon = mbrCouponRepository.fromId(mbrOrderDetail.getMbrHasCouponId() == null ? new MbrCouponId(0) : new MbrCouponId(mbrOrderDetail.getMbrHasCouponId()));
        if (coupon != null) {
            mbrOrderDetail.setDiscountName(coupon.getName());
        }
        return mbrOrderDetail;
    }


    /**
     * 客显获取会员订单详情
     * @param mbrOrderDetailCondition
     * @return 详情
     */
    public MbrOrderDetail aiCashierMbrOrderDetail(MbrOrderDetailCondition mbrOrderDetailCondition) {
        Objects.requireNonNull(mbrOrderDetailCondition.getOrderNumber(),"会员订单编号不能为空");
        MbrOrderDetail mbrOrderDetail = mbrOrderDalMapper.getMbrOrderDetail(mbrOrderDetailCondition);
        if (null == mbrOrderDetail) {
            return null;
        }


        List<MbrRefundDetail> refundInfos;
        //orderPayNumber不为空说明是会员微信/支付宝支付;反之是会员储值卡消费;
        if(!StringUtils.isEmpty(mbrOrderDetail.getOrderPayNumber())){
            MbrRefundDetailstCondition mbrRefundDetailstCondition = new MbrRefundDetailstCondition();
            mbrRefundDetailstCondition.setOrderPayNumber(mbrOrderDetail.getOrderPayNumber());
            mbrRefundDetailstCondition.setId(mbrOrderDetail.getId());
            refundInfos = mbrOrderDalMapper.getCashierMbrOrderRefundInfos(mbrRefundDetailstCondition);
            mbrOrderDetail.setRefundDetails(refundInfos);
            Integer payScore = mbrOrderDalMapper.getMemberOrderPayScore(mbrOrderDetail.getOrderPayNumber());
            mbrOrderDetail.setScore(payScore!=null?payScore:0);
        }else{
            refundInfos = mbrOrderDalMapper.getMbrOrderRefundInfos(mbrOrderDetail.getId());
            mbrOrderDetail.setRefundDetails(refundInfos);
        }
        MbrCoupon coupon = mbrCouponRepository.fromId(mbrOrderDetail.getMbrHasCouponId() == null ? new MbrCouponId(0) : new MbrCouponId(mbrOrderDetail.getMbrHasCouponId()));
        if (coupon != null) {
            mbrOrderDetail.setDiscountName(coupon.getName());
        }
        return mbrOrderDetail;
    }

    /**
     * web查询会员消费记录
     * @param condition
     * @return
     */
    public PagingResult<MbrOrderList> webMbrOrderList(MbrOrderListCondition condition){
        Integer total = mbrOrderDalMapper.countMbrOrder(condition);
        if(total < 1){
            return new PagingResult<MbrOrderList>(total,Collections.EMPTY_LIST);
        }
        List<MbrOrderList> mbrOrderLists = mbrOrderDalMapper.webMbrOrderList(condition);
        return new PagingResult<MbrOrderList>(condition.getPageSize(), condition.getPageNumber(), total, mbrOrderLists);
    }

    public MbrOrderDetail mbrOrderDetail(Long orderId) {
        InMbrOrder inMbrOrder = inMbrOrderMapper.selectByPrimaryKey(orderId);
        if(null == inMbrOrder){
            return null;
        }
        MbrOrderDetailCondition mbrOrderDetailCondition = new MbrOrderDetailCondition();
        mbrOrderDetailCondition.setOrderNumber(inMbrOrder.getOrderNumber());
        MbrOrderDetail mbrOrderDetail = mbrOrderDalMapper.getMbrOrderDetail(mbrOrderDetailCondition);
        if(mbrOrderDetail!=null){
            List<MbrRefundDetail> refundInfos = mbrOrderDalMapper.getMbrOrderRefundInfos(mbrOrderDetail.getId());
            mbrOrderDetail.setRefundDetails(refundInfos);
        }
        return mbrOrderDetail;
    }


    /**
     * 查询会员充值结果
     */
    public MbrOrderRechargeResult queryRechargeResult(Long id){
        MbrOrderRechargeResult result = mbrOrderDalMapper.queryRechargeResult(id);
        return result;
    }

}
